<?php

namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;

/**
 * MonthlyCalendar 日历表信息
 */
class MonthlyCalendar extends Base {

	public function logic() {
		
		$uid = $this->uid;
		//计算今天日期
		$today = date('Y-m-d');
		$year = date('Y');	
		$month = date('m');
		$day = date('d');
		//月份
		$year_month = $year . "年" . $month . "月";
		$yearmonth = $year . $month;

		//本月总天数
		$month_all_days = date("t");
	
		//计算本月第一天是星期几
		$timestamp = strtotime($today);
		$sdata = date('Y-m-d', $timestamp-(date('d', $timestamp)-1)*86400);
		$month_first_weekday = date("w",strtotime($sdata));
		
		
		$data = array();
		$data["today"] = $today;
		$data["day"] = (int)$day;
		$data["year_month"] = $year_month;
		$data["month_all_days"] = (int)$month_all_days;
		$data["month_first_weekday"] = (int)$month_first_weekday;

        $row = DB::select(
            "SELECT * FROM t_signin_rec
			WHERE uid=$uid 
			and create_time >DATE_ADD(CURDATE(),INTERVAL -DAY(CURDATE())+1 DAY) 
			AND create_time <DATE_ADD(CURDATE()-DAY(CURDATE())+1,INTERVAL 1 MONTH )"
		);
		
		$sign_in = array();
		for ($i=1; $i<=$month_all_days; $i++)
		{
			$flag = 0;
			for ($j=0; $j<count($row); $j++)
			{
				if ((int)$row[$j]->day ==  $i )
				{
					$flag = 1;
					break;
				}
			}
			$sign_in[] = $flag;
		}
		$data["sign_in"] = $sign_in;

		$box_info = array(
			array('id'=>3, 'get_flag'=>0),
			array('id'=>7, 'get_flag'=>0),
			array('id'=>15, 'get_flag'=>0),
			array('id'=>21, 'get_flag'=>0),
			array('id'=>28, 'get_flag'=>0)
		);

		//查询本月领取宝箱的所有记录
        $row2 = DB::select(
            "SELECT * FROM t_reward_box_rec 
			WHERE uid=$uid 
			AND create_time >DATE_ADD(CURDATE(),INTERVAL -DAY(CURDATE())+1 DAY)
			AND create_time <DATE_ADD(CURDATE()-DAY(CURDATE())+1,INTERVAL 1 MONTH )"
		);
		foreach ( $box_info as $key => $value )
		{
			for ( $j=0; $j<count($row2); $j++ )
			{
				if ((int)$row2[$j]->get_box_type == (int)$value['id'] )
				{
					$box_info[$key]['get_flag'] = 1;
					break;
				}				
			}
		}
		$data['box_info'] = $box_info;
		return $this->returnData($data);
	}	

}